package com.macro.mall.product.controller;

import com.google.api.client.util.Maps;
import com.macro.mall.common.api.CommonPage;
import com.macro.mall.common.api.CommonResult;
import com.macro.mall.product.dto.PmPrptyPackageDto;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.client.RestTemplate;
import org.springframework.web.util.UriComponentsBuilder;

import java.util.Map;

/**
 * 属性管理Controller
 * Created by macro on 2018/4/26.
 */
@Slf4j
@Controller
@Api(tags = "PmPrptyPackageController", description = "品类包管理")
@RequestMapping("/prptypackage")
public class PmPrptyPackageController {

    @Autowired
    private RestTemplate restTemplate;

    @Value("${host.mall.product}")
    private String HOST_MALL_PRODUCT;

    private String SERVICE_NAME="/prptypackage";

    @ApiOperation(value = "分页获取属性列表")
    @RequestMapping(value = "/list", method = RequestMethod.GET)
    @ResponseBody
    public CommonResult<CommonPage<PmPrptyPackageDto>> list(@RequestParam(value = "pageNum", defaultValue = "1") Integer pageNum,
                                                            @RequestParam(value = "pageSize", defaultValue = "5") Integer pageSize,
                                                            @RequestParam(value = "packageName", defaultValue = "") String packageName
    ) {
        String url = HOST_MALL_PRODUCT +SERVICE_NAME+ "/list";

        UriComponentsBuilder builder = UriComponentsBuilder.fromHttpUrl(url);
        Map<String,Object> param = Maps.newHashMap();
        param.put("pageNum",pageNum);
        param.put("pageSize",pageSize);
        param.put("packageName",packageName);

        param.entrySet().stream().forEach(o -> builder.queryParam(o.getKey(),o.getValue()));
        String restUrl = builder.build().encode().toString();
        log.debug("★★★★★★PmPrptyPackageController.list   restUrl="+restUrl);

        ResponseEntity<CommonResult> responseEntity = restTemplate.getForEntity(restUrl, CommonResult.class);
        return responseEntity.getBody();
    }

    @ApiOperation(value = "保存品类包")
    @RequestMapping(value = "/save", method = RequestMethod.POST)
    @ResponseBody
    public CommonResult save(@Validated @RequestBody PmPrptyPackageDto packageDto, BindingResult result) {
        String url = HOST_MALL_PRODUCT +SERVICE_NAME+ "/save";
        ResponseEntity<CommonResult> responseEntity = restTemplate.postForEntity(url, packageDto, CommonResult.class);
        return responseEntity.getBody();
    }
    @ApiOperation(value = "删除品类包")
    @RequestMapping(value = "/deletePrptyPackage", method = RequestMethod.GET)
    @ResponseBody
    public CommonResult deletePrptyPackage(@RequestParam(value = "packageIds") String packageIds) {
        String url = HOST_MALL_PRODUCT +SERVICE_NAME+ "/deletePrptyPackage";

        UriComponentsBuilder builder = UriComponentsBuilder.fromHttpUrl(url);
        Map<String,Object> param = Maps.newHashMap();
        param.put("packageIds",packageIds);

        param.entrySet().stream().forEach(o -> builder.queryParam(o.getKey(),o.getValue()));
        String restUrl = builder.build().encode().toString();
        log.debug("★★★★★★PmPrptyPackageController.deletePrptyPackage   restUrl="+restUrl);

        ResponseEntity<CommonResult> responseEntity = restTemplate.getForEntity(restUrl, CommonResult.class);
        return responseEntity.getBody();
    }

    @ApiOperation(value = "获取单个品类包")
    @RequestMapping(value = "/getPackageById", method = RequestMethod.GET)
    @ResponseBody
    public CommonResult<PmPrptyPackageDto> getPackageById(@RequestParam(value = "packageId", defaultValue = "") Long packageId
    ) {
        String url = HOST_MALL_PRODUCT +SERVICE_NAME+ "/getPackageById";

        UriComponentsBuilder builder = UriComponentsBuilder.fromHttpUrl(url);
        Map<String,Object> param = Maps.newHashMap();
        param.put("packageId",packageId);

        param.entrySet().stream().forEach(o -> builder.queryParam(o.getKey(),o.getValue()));
        String restUrl = builder.build().encode().toString();
        log.debug("★★★★★★PmPrptyPackageController.getPackageById   restUrl="+restUrl);


        ResponseEntity<CommonResult> responseEntity = restTemplate.getForEntity(restUrl, CommonResult.class);
        return responseEntity.getBody();
    }
}
